/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.p10.miage.gestionnote.action;

import fr.p10.miage.gestionnote.model.EcIntervenant;
import fr.p10.miage.gestionnote.model.EcsIntervenant;
import fr.p10.miage.gestionnote.modelservice.ControleServices;
import fr.p10.miage.gestionnote.modelservice.EcsIntervenantServices;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionRedirect;

/**
 *
 * @author thkeroua
 */
public class ECPublierNoteAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "publier-note-success";
    private static final String ERROR = "publier-note-error";


    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        int idec = Integer.parseInt(request.getParameter("idec"));
        int idControle = Integer.parseInt(request.getParameter("idControle"));
        int idP = Integer.parseInt(request.getParameter("idp"));
        
        EcsIntervenant listeEcsIntervenant = (EcsIntervenant) request.getSession().getAttribute("listeEcsIntervenant");
        
        EcIntervenant ecIntervenant = listeEcsIntervenant.getEcIntervenantById(idec,idP);
        
        ActionMessages errors = new ActionMessages();    

        int nbEtudiant = EcsIntervenantServices.getEtudiant(idec,ecIntervenant.getAnneePromotion()).size();
        
        if(nbEtudiant!=ControleServices.calculNbEtudiantNote(idControle)+ControleServices.calculNbEtudiantDispense(idControle))
        {
             errors.add("publier", new ActionMessage("errors.publier.controle", "controle"));
             saveErrors(request.getSession(), errors);
             ActionRedirect redirect = new ActionRedirect(mapping.findForward(ERROR));
             redirect.addParameter("idec", idec);
             redirect.addParameter("idp", idP);
             return redirect;
        }
        else
        {
            ControleServices.publierNote(idec, idControle);
        }
        
        ActionMessages messages = new ActionMessages();
        messages.add("succesInsertPublierControle", new ActionMessage("succes.publier.controle","controle"));
        saveMessages(request.getSession(), messages);
        
        ActionRedirect redirect = new ActionRedirect(mapping.findForward(SUCCESS));
        redirect.addParameter("idec", idec);
        redirect.addParameter("idp", idP);
        return redirect;
    }
}
